
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  sort;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;

import logger.PoolLogger;
import dbmanager.DBManager;
import sort.Sortinterface;

import utility.CapitalChar;


public class Sortby  
{
	
	String buffer[];
	String FILENAME ="C:\\Item\\updated.csv";
	//private String inputpath;
	//String FILENAME ="//home//osc//Desktop//Item List//Item//sortby.txt"; //input file
	//String OUTPUTFILE = "//home//mahendra//output.txt"; //output file
	PoolLogger pl;
	
	public Sortby()
	{
		System.out.println("i m in sortby method");
		getPath();		
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	public void addReport(Vector<Object> tempmap,String order) 
	{	
		try 
		{
			//String classname ="Descending";
			String classname =order;
			System.out.println("order of sorting-----"+classname);
			pl.info("order of sorting-----"+classname);
			pl.debug("order of sorting-----"+classname);
			pl.warn("order of sorting-----"+classname);
			pl.fatal("order of sorting-----"+classname);
			pl.error("order of sorting-----"+classname);
			pl.trace("order of sorting-----"+classname);
			System.out.println("index of sorting-----"+tempmap);
			pl.info("index of sorting-----"+tempmap);
			pl.debug("index of sorting-----"+tempmap);
			pl.warn("index of sorting-----"+tempmap);
			pl.fatal("index of sorting-----"+tempmap);
			pl.error("index of sorting-----"+tempmap);
			pl.trace("index of sorting-----"+tempmap);
			System.out.println("input file for sorting------"+FILENAME);
			pl.info("input file for sorting------"+FILENAME);
			pl.debug("input file for sorting------"+FILENAME);
			pl.warn("input file for sorting------"+FILENAME);
			pl.fatal("input file for sorting------"+FILENAME);
			pl.error("input file for sorting------"+FILENAME);
			pl.trace("input file for sorting------"+FILENAME);
			Sortinterface sort;
			Class c = Class.forName("sort."+CapitalChar.makeFirstCharCapital(classname));
			sort = (Sortinterface) c.newInstance();
			sort.setbuffer(buffer);
			sort.setVector(tempmap,1);
			sort.getResult();
			buffer = sort.getbuffer();
			File file =new File(FILENAME); 
			RandomAccessFile raf =new RandomAccessFile(file,"rws");
			//RandomAccessFile raf =new RandomAccessFile(OUTPUTFILE,"rws");
			//System.out.println("after sorting");
			for(int i=0;i<buffer.length;i++)
			{
				raf.writeBytes(buffer[i]+"\n");
				System.out.println(buffer[i]);
			}
		}
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public void initializeData() 
	{	
		try
		{
			System.out.println(FILENAME);
			File file =new File(FILENAME); 
			RandomAccessFile raf =new RandomAccessFile(file,"r");
			String result="";
			StringBuffer tembuffer =new StringBuffer();
			while((result=raf.readLine())!=null)
			{
				tembuffer.append(result+"\n");
			}
			System.out.println("buffer before sorting"+tembuffer);
			pl.info("buffer before sorting"+tembuffer);
			pl.debug("buffer before sorting"+tembuffer);
			pl.warn("buffer before sorting"+tembuffer);
			pl.fatal("buffer before sorting"+tembuffer);
			pl.error("buffer before sorting"+tembuffer);
			pl.trace("buffer before sorting"+tembuffer);
			buffer =tembuffer.toString().split("\n");
			raf.close();
			file.delete();
			file.createNewFile();
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	public void SetPath(String path)
	{
		this.FILENAME=path;		
	}
	public String getPath()
	{
		return FILENAME;		
	
	}
	

	public static void main(String[] args) 
	{
		Sortby sb = new Sortby();
		Vector<Object> temp =new Vector<Object>();
		String order="Ascending";
		temp.add(0);
	
		sb.initializeData();
		sb.addReport(temp,order);
		
	}
}
